/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.settingsgui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import softstarter.functionality.FileCentre;
import softstarter.functionality.RunnableItem;
import softstarter.gui.SoftStarter;

public class FilesAndButtons extends JPanel {
	
	private FileCentre fileCentre;
	private SoftStarter settingsWindow;
	private HashMap<JButton, RunnableItem> filesAndButtons;
	private RemoveButtonListener removeListener;
	
	public FilesAndButtons(FileCentre fileCentre, SoftStarter settingsWindow) {
		this.fileCentre = fileCentre;
		this.settingsWindow = settingsWindow;
		removeListener = new RemoveButtonListener();
		buildListWithFilesAndRemoveButtons();
	}
	
	public void buildListWithFilesAndRemoveButtons() {
		filesAndButtons = new HashMap<JButton, RunnableItem>();
		this.removeAll();
		
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		this.setLayout(layout);
		
		constraints.gridx = 0; constraints.gridy = 0;
		this.add(new JLabel("Drag&Drop files to this window!"), constraints);
		
		constraints.gridx = 0; constraints.gridy = 2;
		System.out.println(fileCentre.getFiles().size());
		for(RunnableItem file : fileCentre.getFiles()) {
			this.add(new JLabel(file.getName()), constraints);
			constraints.gridx ++;
			JButton removeButton = new JButton("Remove");
			removeButton.addActionListener(removeListener);
			this.add(removeButton, constraints);
			filesAndButtons.put(removeButton, file);
			constraints.gridx --; constraints.gridy ++;
			filesAndButtons.put(removeButton, file);
		}
		
	}
	
	private class RemoveButtonListener implements ActionListener {

		public RemoveButtonListener() {
			
		}
		
		@Override
		public void actionPerformed(ActionEvent event) {

			fileCentre.removeFile(filesAndButtons.get(event.getSource()));
			filesAndButtons.remove(event.getSource());
			settingsWindow.updateFileList();
			
		}
		
	}
	
}
